package cn.edu.lingnan.tag;

import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.tagext.BodyTagSupport;
import jakarta.servlet.jsp.tagext.BodyContent;

public class TagExample3 extends BodyTagSupport {
    private int count;
    private String name;
    public void setCount(int n){ count = n; }
    public int getCount() { return count; }
    public void setName(String name){ this.name=name; }
    public String getName() { return this.name; }

    public int doStartTag() throws JspException {
    	System.out.println("the doStartTag mothed...");
         if (count>0){
        	 System.out.println("the count in doStartTag value is:"+count);
        	 return EVAL_BODY_BUFFERED;
         }else
        	 return SKIP_BODY;
    }

    public int doAfterBody() throws JspException{
		System.out.println("the count in doAfterBody value is:"+count);
         if(count>1){
              count--;
              return EVAL_BODY_AGAIN;
         }
         return SKIP_BODY;
    }

	public int doEndTag() throws JspException{
		System.out.println("the doEngTag mothed...");
		BodyContent bc = getBodyContent();
		if (bc!=null){
			try{
				pageContext.getOut().print("Hello:" + name);
				bc.writeOut( bc.getEnclosingWriter());
			}catch(java.io.IOException e){
				e.printStackTrace();
			}
		}
        return EVAL_PAGE;
   }

	public void setBodyContent(BodyContent bodycontent){
		System.out.println("the setBodyContent mothed...");
		this.bodyContent=bodycontent;
	}

}